<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
      style="overflow: hidden"
    >
      <el-form-item label="所属医院" prop="doctorNo">
        <el-select
          v-model="queryParams.param.hospitalId"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in hospitalList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="doctorNo">
        <el-input
          v-model="queryParams.param.name"
          controls-position="right"
          placeholder="请输入"
          clearable
          build:prod
        />
      </el-form-item>

      <el-form-item label="手机号" prop="doctorNo">
        <el-input
          v-model="queryParams.param.phone"
          controls-position="right"
          placeholder="请输入"
          build:prod
          clearable
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="doctorNo">
        <el-input
          v-model="queryParams.param.idCard"
          controls-position="right"
          placeholder="请输入"
          build:prod
          clearable
        />
      </el-form-item>
      <el-form-item label="筛查结果" prop="dangeStatus">
        <el-select
          v-model="queryParams.param.dangeStatus"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="加密/未加密" prop="doctorNo">
        <el-select
          v-model="queryParams.param.isDesensitization"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option label="加密" :value="1"> </el-option>
          <el-option label="未加密" :value="0"> </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="创建时间" prop="doctorNo">
        <el-date-picker
          v-model="queryTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item style="float: right; margin-right: 0">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport()"
            v-hasPermi="['screening:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </div>
    <el-table
      max-height="600"
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createdBy"
        min-width="150"
        fixed
      >
        <template slot-scope="scope">{{ scope.row.createdAt }} </template>
      </el-table-column>
      <el-table-column
        label="医院名称"
        align="center"
        prop="hospitalName"
        show-overflow-tooltip
        width="150"
        fixed
      />
      <el-table-column
        label="科室名称"
        align="center"
        prop="SCWJ-DEPT"
        show-overflow-tooltip
        width="100"
        fixed
      />
      <el-table-column
        label="联系方式"
        align="center"
        prop="SCWJ-PHONE"
        show-overflow-tooltip
        width="120"
        fixed
      />
      <el-table-column
        label="身份证号"
        align="center"
        prop="SCWJ-idCard"
        show-overflow-tooltip
        width="180"
        fixed
      />
      <el-table-column
        label="姓名"
        align="center"
        prop="SCWJ-NAME"
        show-overflow-tooltip
        width="120"
        fixed
      />
      <el-table-column
        label="性别"
        align="center"
        prop="SCWJ-SEX"
        show-overflow-tooltip
      />
      <el-table-column
        label="年龄"
        align="center"
        prop="SCWJ-AGE"
        show-overflow-tooltip
      />
      <el-table-column
        label="出生日期"
        align="center"
        prop="SCWJ-BIRTH"
        show-overflow-tooltip
        width="100"
      />
      <!-- <el-table-column
        label="身份证号"
        align="center"
        prop="SCWJ-idCard"
        show-overflow-tooltip
        width="180"
      >
        <template #header>
          <div class="template-header">
            <span>身份证号</span>
            <i
              class="el-icon-view"
              v-if="isIdcard"
              @click="isIdcard = false"
            ></i>
            <img
              src="./by.png"
              alt=""
              width="13"
              height="13"
              v-if="!isIdcard"
              @click="isIdcard = true"
            />
          </div>
        </template>
        <template slot-scope="scope">
          {{ isIdcard ? scope.row["SCWJ-idCard"] : scope.row["idCardEncrypt"] }}
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="手机号"
        align="center"
        prop="SCWJ-PHONE"
        width="150"
        show-overflow-tooltip
      >
        <template #header>
          <div class="template-header">
            <span>手机号</span>
            <i class="el-icon-view" v-if="isPhone" @click="isPhone = false"></i>
            <img
              src="./by.png"
              alt=""
              width="13"
              height="13"
              v-if="!isPhone"
              @click="isPhone = true"
            />
          </div>
        </template>
        <template slot-scope="scope">
          {{ isPhone ? scope.row["SCWJ-PHONE"] : scope.row["phoneEncrypt"] }}
        </template>
      </el-table-column> -->

      <el-table-column
        label="筛查结果"
        align="center"
        min-width="100"
        fixed="right"
      >
        <template slot-scope="scope">
          <span v-if="scope.row['SCWJ-RESULT'] - 0 == 0" style="color: #78d12c"
            >正常</span
          >
          <span v-if="scope.row['SCWJ-RESULT'] - 0 == 1" style="color: #0096ff"
            >低危</span
          >
          <span v-if="scope.row['SCWJ-RESULT'] - 0 == 2" style="color: #ff864b"
            >中危</span
          >
          <span v-if="scope.row['SCWJ-RESULT'] - 0 == 3" style="color: #ff3131"
            >高危</span
          >
          <span></span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="加密/未加密"
        align="center"
        width="100"
        fixed="right"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.isDesensitization - 0 == 0">未加密</span>
          <span v-if="scope.row.isDesensitization - 0 == 1">加密</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="数据来源"
        align="center"
        min-width="150"
        fixed="right"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ scope.row["SCWJ-SQMC"] || scope.row.hospitalName }}
        </template>
      </el-table-column>
      <el-table-column
        label="有高血压，>=140/90mmHg"
        align="center"
        min-width="200"
      >
        <template slot-scope="scope">
          {{
            scope.row["SCWJ-ILLNESS"]
              ? scope.row["SCWJ-ILLNESS"].includes("01")
                ? "是"
                : "否"
              : "否"
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="血脂异常，或不清楚"
        align="center"
        min-width="140"
      >
        <template slot-scope="scope">
          {{
            scope.row["SCWJ-ILLNESS"]
              ? scope.row["SCWJ-ILLNESS"].includes("02")
                ? "是"
                : "否"
              : "否"
          }}
        </template>
      </el-table-column>
      <el-table-column label=" 有糖尿病  " align="center" min-width="100">
        <template slot-scope="scope">
          {{
            scope.row["SCWJ-ILLNESS"]
              ? scope.row["SCWJ-ILLNESS"].includes("03")
                ? "是"
                : "否"
              : "否"
          }}</template
        >
      </el-table-column>
      <el-table-column label=" 心跳不规则 " align="center" min-width="100">
        <template slot-scope="scope">
          {{
            scope.row["SCWJ-ILLNESS"]
              ? scope.row["SCWJ-ILLNESS"].includes("04")
                ? "是"
                : "否"
              : "否"
          }}</template
        >
      </el-table-column>
      <el-table-column label=" 吸烟 " align="center" min-width="60">
        <template slot-scope="scope">
          {{
            scope.row["SCWJ-ILLNESS"]
              ? scope.row["SCWJ-ILLNESS"].includes("05")
                ? "是"
                : "否"
              : "否"
          }}</template
        >
      </el-table-column>
      <el-table-column label=" 明显超重或肥胖 " align="center" min-width="122">
        <template slot-scope="scope">
          {{
            scope.row["SCWJ-ILLNESS"]
              ? scope.row["SCWJ-ILLNESS"].includes("06")
                ? "是"
                : "否"
              : "否"
          }}
        </template>
      </el-table-column>
      <el-table-column label=" 缺乏运动 " align="center" min-width="100">
        <template slot-scope="scope">
          {{
            scope.row["SCWJ-ILLNESS"]
              ? scope.row["SCWJ-ILLNESS"].includes("07")
                ? "是"
                : "否"
              : "否"
          }}
        </template>
      </el-table-column>
      <el-table-column label=" 有卒中家族史 " align="center" min-width="122">
        <template slot-scope="scope">
          {{
            scope.row["SCWJ-ILLNESS"]
              ? scope.row["SCWJ-ILLNESS"].includes("08")
                ? "是"
                : "否"
              : "否"
          }}
        </template>
      </el-table-column>
      <el-table-column
        label=" 有既往脑卒中病史 "
        align="center"
        min-width="130"
      >
        <template slot-scope="scope">
          {{
            scope.row["SCWJ-ILLNESS"]
              ? scope.row["SCWJ-ILLNESS"].includes("09")
                ? "是"
                : "否"
              : "否"
          }}
        </template>
      </el-table-column>
      <el-table-column
        label=" 有既往短暂脑缺血病史 "
        align="center"
        min-width="150"
      >
        <template slot-scope="scope">
          {{
            scope.row["SCWJ-ILLNESS"]
              ? scope.row["SCWJ-ILLNESS"].includes("10")
                ? "是"
                : "否"
              : "否"
          }}
        </template>
      </el-table-column>

      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            id="inputName"
            size="mini"
            type="success"
            plain
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['address1:upd']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            plain
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['address1:del']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改岗位对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="640px"
      append-to-body
      class="popup"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="52px"
        class="formStep"
      >
        <el-form-item label="位置" prop="address">
          <el-input v-model="form.address" placeholder="请输入"> </el-input>
        </el-form-item>
        <el-form-item label="编号" prop="addressNo">
          <el-input v-model="form.addressNo" placeholder="请输入"> </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryDetailList,
  queryHospital,
  exportData,
} from "@/api/data/archives";
export default {
  name: "Post",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      isIdcard: false,
      isPhone: false,
      options: [
        // {
        //   value: "0",
        //   label: "正常",
        // },
        {
          value: "1",
          label: "低危",
        },
        {
          value: "2",
          label: "中危",
        },
        {
          value: "3",
          label: "高危",
        },
      ],
      queryList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // sort: 'cd.create_time desc',
        param: {
          name: "",
          phone: "",
          idCard: "",
          dangeStatus: "",
          hospitalId: "",
          isDesensitization: "",
          startTime: "",
          endTime: "",
        },
      },
      // 表单参数
      form: {
        address: "",
        addressNo: "",
      },
      // 表单校验
      rules: {
        addressNo: [
          { required: true, message: "编号不能为空", trigger: "blur" },
        ],
        address: [{ required: true, message: "位置不能为空", trigger: "blur" }],
      },
      hospitalList: [],
    };
  },
  created() {
    // this.getLastWeekDays();
    this.getList(); //表格列表
    this.getHospital();
  },
  methods: {
    /** 所属医院 */
    getHospital() {
      queryHospital({
        pageNum: -1,
        param: {},
      }).then((response) => {
        this.hospitalList = response.data.list;
        // this.$refs.table.doLayout()
      });
    },
    judgePhone(val) {
      if (!val) return "";
      let reg = /^(.{3}).*(.{4})$/;
      return val.replace(reg, "$1****$2");
    },
    judgeCard(val) {
      if (!val) return "";
      let reg = /^(.{6}).*(.{4})$/;
      return val.replace(reg, "$1********$2");
    },
    // 获取上一周的开始结束时间
    getLastWeekDays() {
      let obj = {
        startTime: "",
        endTime: "",
      };
      const nowTime = this.$moment().valueOf();
      obj.startTime = this.$moment(nowTime - 60 * 60 * 24 * 30 * 1000).format(
        "YYYY-MM-DD"
      );
      obj.endTime = this.$moment().format("YYYY-MM-DD");
      this.queryTime = [obj.startTime, obj.endTime];
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      // 判断时间数组是否有值
      this.queryParams.param.startTime = "";
      this.queryParams.param.endTime = "";
      if (this.queryTime?.length) {
        this.queryParams.param.startTime = this.queryTime[0] + " " + "00:00:00";
        this.queryParams.param.endTime = this.queryTime[1] + " " + "23:59:59";
      }
      queryDetailList(this.queryParams).then((response) => {
        this.postList = response.data.list;
        this.postList.forEach((item) => {
          if (item.recordList) {
            item.recordList.forEach((row) => {
              item[row.code] = row.answer;
              // 8+2答案
              if (row.code == "SCWJ-ILLNESS") {
                item[row.code] = row.answer.split(",");
              }
              // 身份证号脱敏
              if (row.code == "SCWJ-idCard") {
                item["idCardEncrypt"] = this.judgeCard(row.answer);
              }
              // 手机号脱敏
              if (row.code == "SCWJ-PHONE") {
                item["phoneEncrypt"] = this.judgePhone(row.answer);
              }
            });
          }
        });
        this.total = response.data.total;
        this.loading = false;
        // this.$refs.table.doLayout()
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        address: "",
        addressNo: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.param = {
        name: "",
        phone: "",
        idCard: "",
        dangeStatus: "",
        hospitalId: "",
        isDesensitization: "",
        startTime: "",
        endTime: "",
      };
      this.queryTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row));
      this.title = "修改";
      this.open = true;
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(this.form));
          if (data.id != undefined) {
            updAddress(data).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAddress(data).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.id ? [row.id] : this.ids;
      this.$modal
        .confirm("是否确认删除数据？")
        .then(function () {
          return;
        })
        .then(() => {
          delAddress({ idList: postIds }).then((response) => {
            this.$modal.msgSuccess("删除成功");
            this.open = false;
            this.getList();
          });
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // let data = JSON.parse(JSON.stringify(this.queryParams));
      // data.param.idList = this.ids;
      // this.download(
      //   "screen/exportRzScreen",
      //   {
      //     ...data,
      //   },
      //   `卒中高危人群筛查.xlsx`
      // );

      const that = this;
      this.$modal.confirm("是否确认导出数据？").then(() => {
        // 导出
        const params = that.queryParams;
        params.param.idList = this.ids;
        exportData({ ...params })
          .then((res) => {
            // const url = window.URL.createObjectURL(new Blob([res]));
            const blob = new Blob([res]);
            this.$download.saveAs(blob, "卒中高危人群筛查.xlsx");
            this.$message.success("导出成功");
          })
          .catch((error) => {
            this.$message.success("下载Excel失败:", error);
            console.error("下载Excel失败:", error);
          });
      });
    },
  },
};
</script>
<style scoped src="@/assets/styles/common.css"></style>
<style scoped>
.template-header {
  display: flex;
  align-items: center;
  justify-content: center;
}
.template-header span {
  margin-right: 5px;
}
a {
}
</style>
